package com.huyanqi.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * <p>Title: Page.java</p>
 * 
 * <p>Description: 封装分页对象</p>
 * 
 * <p>Datetime: 2011-12-25 下午11:34:12</p>
 *
 * @company 成都映潮科技有限公司
 * 
 * @author <a href="mailto:liwei@sunsharp.cn">p.k.ripper</a>
 *
 */
public class Page<T> implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	//-- 公共变量 --//
	public static final String ASC = "asc";
	public static final String DESC = "desc";

	//-- 分页参数 --//
	
	private int pageNum = 1; //当前页码
	private int pageSize = 10; //每页显示
	private String orderBy = null;
	private String order = null;
	private boolean autoCount = true;
	private int firstResult=-1;
	
	private String url = null;//分页的链接地址
	
	private List<T> result =new ArrayList<T>();
	
	private long totalCount = -1;
	private int totalPages = -1;
			
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getTotalPages() {
		if (totalCount < 0) {
			return -1;
		}
		int count = (int)(totalCount / pageSize);
		if (totalCount % pageSize > 0) {
			count++;
		}
		totalPages=count;
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	public Page(){}

	public int getPageNum() {
		return pageNum;
	}
	//当前页小于1时自动调整为1
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
		if (pageNum < 1) {
			this.pageNum = 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize<0)pageSize=0;
		this.pageSize = pageSize;
	}
	
	/**
	 * 返回Page对象自身的setPageSize函数,可用于连续设置。
	 */
	public Page<T> pageSize(final int thePageSize) {
		setPageSize(thePageSize);
		return this;
	}
	
	/**
	 * 获得排序字段,无默认值. 多个排序字段时用','分隔.
	 */
	public String getOrderBy() {
		return orderBy;
	}
	/**
	 * 设置排序字段,多个排序字段时用','分隔.
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	/**
	 * 返回Page对象自身的setOrderBy函数,可用于连续设置。
	 */
	public Page<T> orderBy(final String theOrderBy) {
		setOrderBy(theOrderBy);
		return this;
	}
	
	public String getOrder() {
		return order;
	}
	/**
	 * 设置排序方式向.
	 * 
	 * @param order 可选值为desc或asc,多个排序字段时用','分隔.
	 */
	public void setOrder(String order) {
		String lowcaseOrder = StringUtils.lowerCase(order);

		//检查order字符串的合法值
		String[] orders = StringUtils.split(lowcaseOrder, ',');
		for (String orderStr : orders) {
			if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
				throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
			}
		}

		this.order = lowcaseOrder;
	}
	/**
	 * 是否已设置排序字段,无默认值.
	 */
	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
	}
	/**
	 * 获得查询对象时是否先自动执行count查询获取总记录数, 默认为true.
	 */
	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}
	/**
	 * 返回Page对象自身的setAutoCount函数,可用于连续设置。
	 */
	public Page<T> autoCount(final boolean theAutoCount) {
		setAutoCount(theAutoCount);
		return this;
	}
	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从0开始.
	 */
	public int getFirst() {
		if(firstResult!=-1)return firstResult;
		else return ((pageNum - 1) * pageSize);
	}
	
	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext() {
		return (pageNum + 1 <= getTotalPages());
	}
	
	/**
	 * 取得下页的页号, 序号从1开始.
	 * 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextPage() {
		if (isHasNext()) {
			return pageNum + 1;
		} else {
			return pageNum;
		}
	}
	
	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPre() {
		return (pageNum - 1 >= 1);
	}

	/**
	 * 取得上页的页号, 序号从1开始.
	 * 当前页为首页时返回首页序号.
	 */
	public int getPrePage() {
		if (isHasPre()) {
			return pageNum - 1;
		} else {
			return pageNum;
		}
	}
}
